package com.boxedinsoftware.betamax.dial;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.view.KeyEvent;

public class dialNowPrefs extends PreferenceActivity {
	SharedPreferences preferences; 
	private PreferenceScreen screen;
	protected boolean hasChanged = false;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    addPreferencesFromResource(R.xml.prefs);
	    
	    hasChanged = false;
	    screen = getPreferenceScreen();
	    preferences = PreferenceManager.getDefaultSharedPreferences(this);
	    
	    Preference accounts = screen.findPreference("accounts");
	    
	    accounts.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			public boolean onPreferenceClick(Preference arg0) {
				
				Intent i = new Intent(dialNowPrefs.this, AccountManager.class);
    			startActivity(i);
				
				return true;
			}
	    	
	    	
	    	
	    });
	    
	    final Preference defPrefix = screen.findPreference("defaultPrefix");
	    //Preference dDialer = screen.findPreference("directDialer");
	    
	    final CheckBoxPreference dDialer = (CheckBoxPreference) screen.findPreference("directDialer"); 
	    
	    if(dDialer.isChecked())
	    {
	    
	    	defPrefix.setEnabled(false);
	    	
	    }
	    else
	    	defPrefix.setEnabled(true);
	    
	    dDialer.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference arg0) {
			
				if(dDialer.isChecked() == true)
				{
					defPrefix.setEnabled(false);
					return false;
					
				}
				else
				{
					defPrefix.setEnabled(true);
					return true;
					
				}
				
			}
	    	
	    	
	    });
	    
	    
	    
	    final CheckBoxPreference cbDebug = (CheckBoxPreference)screen.findPreference("callbackDebug");
	    
	    cbDebug.setOnPreferenceClickListener(new OnPreferenceClickListener()
	    {

			public boolean onPreferenceClick(Preference preference) {
				
				if (cbDebug.isChecked() == false)
				{return true;}
				else
				{
					
					AlertDialog.Builder builder = new AlertDialog.Builder(dialNowPrefs.this);
					builder.setMessage("CallBack Debug might contain personal information like your BetaMax username, password, destination or source number." +
							"BoxedInSoftware will only use the provided information for debugging purposes only and will not make use of ANY personal data." +
							"\n" +
							"\n" +
							"In order to use this feature you must AGREE that you have read the above warning." +
							"\n" +
							"\n" +
							"BoxedInSoftware")
					       .setCancelable(false)
					       .setPositiveButton("I AGREE", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                cbDebug.setChecked(true);
					           }
					       })
					       .setNegativeButton("I DISAGREE", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					        	   cbDebug.setChecked(false);
					           }
					       });
					AlertDialog alert = builder.create();	
					alert.show();
					
				return true;
				}
				
				
			}
	    	
	    	
	    });
	    
	    
	    
	    final EditTextPreference ownNumberPref = (EditTextPreference)screen.findPreference("ownNumber");
	    
	    ownNumberPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			public boolean onPreferenceChange(Preference arg0, Object arg1) {
				final CheckBoxPreference ownNumberAutoPref = (CheckBoxPreference)screen.findPreference("autoFetchOwnNumber");
				ownNumberAutoPref.setChecked(false);
				
				return true;
			}
	    	
	    	
	    });
	    
	    
	    	
	   
	    
	}
	
	 @Override
	    public boolean onKeyDown(int keyCode, KeyEvent event) {
	        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	        	this.getIntent().putExtra("refresh", 1);
	        		
	        			setResult(Activity.RESULT_OK, null);
	        		
	        			
	    	    finish();
	        
	        
	        }
	        return super.onKeyDown(keyCode, event);
	    }
	

}